<?php

namespace App\Console\Commands;

use App\Http\Controllers\Admin\CommonController;
use App\Library\TcScfUtil;
use App\Library\TencentHandle;
use App\Library\UpTools;
use App\Models\Admin\Configs;
use App\Models\Saas\Mall;
use Illuminate\Console\Command;

class HandleData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'javascript:handle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $common = new CommonController();
        $common->updateArticle();
        $common->updateAfterSale();
        $common->updateActivityStatus();
        $common->customerUpdateInfo();
        $common->updateRemindTime();
        $common->recordOperateData();
        $common->CompanyOperateData();
        $common->personalOperateData();
    }

    public function temp(){
        $page = 0;
        $limit = 1000;
        $count = 0;
        $data = [];
        $con = Configs::query()
            ->where("mall_id", 1)
            ->select("tencent_secretid", "tencent_secrekey")
            ->first();
        do{
            $requestParams = [
                "OrderDirection" => "DESC", 'Offset' => $page * $limit, 'Limit' => $limit, "HasOverdueBill" => 1
            ];
            $res = TcScfUtil::getInstance($con->tencent_secretid, $con->tencent_secrekey, "partner")
                ->describeAgentAuditedClients($requestParams);
            $page++;
            if(!empty($res["data"]["AgentClientSet"])){
                foreach ($res["data"]["AgentClientSet"] as &$item){
                    $client = TcScfUtil::getInstance($con->tencent_secretid, $con->tencent_secrekey, "partner");
                    $result = $client->describeClientBalance(['ClientUin' => $item["ClientUin"]]);
                    $item['balance'] = isset($result['data']['Balance']) ? $result['data']['Balance'] / 100 : 0;
                    $result = $client->describeAgentClientGrade($item["ClientUin"]);
                    $item['ClientGrade'] = !empty($result['data']['ClientGrade']) ? $result['data']['ClientGrade'] : '-';
                }
                $res["data"]["AgentClientSet"] = UpTools::transferFields($res["data"]["AgentClientSet"]);
                $count = $count + count($res["data"]["AgentClientSet"]);
                $data = array_merge($data, $res["data"]["AgentClientSet"]);
            }
        }while($count < $res["data"]["TotalCount"]);
        echo count($data);
        $headlist = [
            "客户账号ID", "客户账号ID", "客户备注", "客户名称"	, "审核时间", "上月消费金额",
            "本月消费金额", "是否欠费", "客户类型",
            "业务员ID", "业务员姓名", "客户邮箱", "余额", "等级"
        ];
        $keyName = [
            "uin", "clientUin", "clientRemark", "clientName", "agentTime",
            "lastMonthAmt", "thisMonthAmt", "hasOverdueBill", "clientType", "salesUin",
            "salesName", "mail", 'balance', 'clientGrade'
        ];
        $exportData = [];
        foreach ($keyName as $key){
            foreach ($data as $i => $item){
                if($key == "agentTime" && isset($item[$key])){
                    $item[$key] = date("Y-m-d H:i:s", $item[$key]);
                }elseif($key == "hasOverdueBill" && isset($item[$key])){
                    $item[$key] = $item[$key] == "0" ? "不欠费" : "欠费";
                }elseif($key == "clientType" && isset($item[$key])){
                    $item[$key] = $item[$key] == "new" ? "新拓" : ($item[$key] == "assign" ? "指定" : "存量");
                }
                $exportData[$i][] = isset($item[$key]) ? $item[$key] : "";
            }
        }
        try {
            TencentHandle::getInstance()->ajaxExport(public_path() . '/tempdata/', "data1.csv", $exportData, $headlist);
        }catch (\Exception $e){
            echo "<pre>";print_r($e->getMessage().$e->getLine().$e->getTraceAsString());die("2");
        }
    }
}
